package card

import (
	"fmt"
	"server/cfg"
	"server/game/bag"
	db3 "server/game/bag/db"
	"server/game/item"
	"server/util"
	"strconv"
	"strings"
)

type CardLogic struct {
	dao *CardDao
}

func NewCardLogic() *CardLogic {
	return &CardLogic{
		dao: NewCardDao(),
	}
}

func (c *CardLogic) cardNengLi(userId string) float64 {
	var nengLi float64 = 0
	cardList, err := c.dao.cardList(userId)
	if err != nil {
		return 0
	}
	for _, cardInfo := range cardList {
		nengLi = nengLi + c.dao.calculateAbility(cardInfo)
	}
	return nengLi
}
func (c *CardLogic) cardNengLiByCardId(userId string, cardId int64) float64 {
	cardInfo, err := c.dao.cardInfoCtid(userId, cardId)
	if err != nil {
		return 0
	}
	return c.dao.getJiChuNengLi(cardInfo)
}

func (c *CardLogic) newCard(userId string, cardId int64) ([]CardInfo, error) {
	cardConfig := cfg.ConfigData.TbCardBase.Get(int32(cardId))
	if cardConfig == nil {
		return nil, fmt.Errorf("模版id错误")
	}
	cardInfo := CardInfo{
		CardId:     util.CreateUUid(),
		UserId:     userId,
		Star:       int64(cardConfig.Color),
		Level:      1,
		SkillList:  make(map[int32]int64, 0),
		WorkList:   make(map[int32]int64, 0),
		FateList:   make(map[int32]int64, 0),
		SkinSkill:  make(map[int32]int64, 0),
		HolaList:   make(map[int32]int64, 0),
		UniqueList: make(map[int32]int64, 0),
		CTId:       cardId,
	}
	for _, cardSkillINfo := range cardConfig.SkillList {
		cardInfo.SkillList[cardSkillINfo] = 1
	}
	err := c.dao.Insert(cardInfo)
	if err != nil {
		return nil, err
	}
	cardDataList, _ := c.dao.cardList(userId)

	return cardDataList, nil
}

func (c *CardLogic) cardListByUserId(userId string) ([]CardInfo, error) {
	return c.dao.cardList(userId)
}
func (c *CardLogic) cardInfoByUserIdAndCardId(userId string, cardId string) (CardInfo, error) {
	return c.dao.cardInfoCardId(userId, cardId)
}
func (c *CardLogic) cardInfoByUserIdAndCtId(userId string, cardId int64) (CardInfo, error) {
	return c.dao.cardInfoCtid(userId, cardId)
}
func (c *CardLogic) upCardLevel(userId string, cardId int64, step int64) (map[string]interface{}, error) {
	var ret = make(map[string]interface{}, 0)
	cardInfo, err := c.dao.cardInfoCtid(userId, cardId)
	if err != nil {
		return ret, err
	}
	cardInfo.Level = cardInfo.Level + step
	config := cfg.ConfigData.TbCardLevel.Get(int32(cardInfo.Level))
	bagList, err := bag.EnUseItem(userId, db3.RencaiExp, int(config.Exp))
	if err != nil {
		return ret, err
	}
	err = c.dao.updateCardLevel(userId, cardId, cardInfo.Level)
	if err != nil {
		return ret, err
	}
	cardDataList, err := c.dao.cardList(userId)
	ret["card_list"] = cardDataList
	ret["bag_list"] = bagList
	return ret, nil
}

func (c *CardLogic) updateCardStar(userId string, cardId, step int64) (map[string]interface{}, error) {
	var ret = make(map[string]interface{}, 0)
	cardInfo, err := c.dao.cardInfoCtid(userId, cardId)
	if err != nil {
		return ret, err
	}
	cardInfo.Star = cardInfo.Star + step
	var bagList []item.Item
	configList := cfg.ConfigData.TbCardUnique.GetDataList()
	for _, cardConfigInfo := range configList {
		if cardConfigInfo.Star == int32(cardInfo.Star) {
			costItem := strings.Split(cardConfigInfo.Consume, "_")
			itemId, _ := strconv.Atoi(costItem[0])
			itemNum, _ := strconv.Atoi(costItem[1])
			_, err := bag.EnUseItem(userId, itemId, itemNum)
			if err != nil {
				return ret, err
			}
		}
	}
	err = c.dao.updateCardStar(userId, cardId, cardInfo.Star)
	cardDataList, _ := c.dao.cardList(userId)
	if err != nil {
		return ret, nil
	}
	bagList, err = bag.EnGetUserItem(userId)
	ret["bag_list"] = bagList
	ret["card_list"] = cardDataList
	return ret, nil
}
func (c *CardLogic) updateSkillLevel(userId string, cardId int64, skillType int, skillId int64, step int64) (CardInfo, error) {
	cardInfo, err := c.dao.cardInfoCtid(userId, cardId)
	var skillInfo = make(map[int32]int64)
	var key = ""
	switch skillType {
	case 1:
		key = "skill_list"
		skillInfo = cardInfo.SkillList
	case 2:
		key = "work_list"
		skillInfo = cardInfo.WorkList
	case 3:
		key = "fate_list"
		skillInfo = cardInfo.FateList
	case 4:
		key = "skin_skill"
		skillInfo = cardInfo.SkinSkill
	case 5:
		key = "hola_list"
		skillInfo = cardInfo.HolaList
	case 6:
		key = "unique_list"
		skillInfo = cardInfo.UniqueList
	}
	skillInfo[int32(skillId)] += step
	switch skillType {
	case 1:
		cardInfo.SkillList = skillInfo
	case 2:
		cardInfo.WorkList = skillInfo
	case 3:
		cardInfo.FateList = skillInfo
	case 4:
		cardInfo.SkinSkill = skillInfo
	case 5:
		cardInfo.HolaList = skillInfo
	case 6:
		cardInfo.UniqueList = skillInfo
	}
	err = c.dao.updateCardSkill(userId, cardId, key, skillInfo)
	if err != nil {
		return cardInfo, err
	}
	return cardInfo, nil
}
